<!DOCTYPE html>
<html>
<head>
#include("../../_common/head.html")
<title>编辑</title>
</head>
<body>
    <div class="main-container">
	    <div id="assign-table"></div>
		<form class="layui-form layui-form-pane" onsubmit="getCheckData();">
		    <input type="hidden" name="bean.id" value="#(bean.id)">
	        <input type="hidden" name="bean.roleId" value="#(bean.roleId)">
		    <input type="hidden" name="bean.resourceIds" value="#(bean.resourceIds)">
		    
		    <table class="layui-hidden" id="beans-table" lay-filter="beans-table"></table>
		    
		    <div class="bottom" style="display: none;">
		        <div class="button-container">
		            <button onclick="window.checkData();" type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="data-save" id="data-save">
		                <i class="layui-icon layui-icon-ok"></i> 提交
		            </button>
		        </div>
		    </div>
		</form>
	</div>
	
    <!-- 图标字段 -->
    <script type="text/html" id="icon">
        <i class="layui-icon {{d.icon}}"></i>
    </script>
    
    <!-- 类型字段 -->
    <script type="text/html" id="typeName">
        {{d.typeName}}
    </script>

#include("../../_common/foot.html")
	<script type="text/javascript">
	layui.use(['jquery', 'form', 'table', 'treetable'], function() {
	    var $ = layui.jquery;
	    var form = layui.form;
	    var table = layui.table;
	    var treetable = layui.treetable;
	    
        // 模块操作路径
        var MODULE_PATH = "#(ctxPath)/admin/system/resource/";

	    /*
         * 刷新表格
         */
        window.renderTable = function(){
            layer.load(2);  //加载层
            var beanIds = [#(bean.resourceIds)];
	        treetable.render({
	            treeColIndex: 2,
	            treeSpid: '0',
	            treeIdName: 'id',
	            treePidName: 'parentId',
	            treeDefaultClose: false,
	            elem: '#beans-table',
	            url: MODULE_PATH + "list",
	            page: false,
	            id: "bean-treetable",
	            where: {"status": "on"},
	            cols: [[
	                {type: 'checkbox'},
	                {field: 'typeName', align: 'center', width: 80, title: '类型', templet: '#typeName'},
	                {field: 'name', align: 'left', width: 320, title: '名称'},
	                {field: 'icon', align: 'center', width: 60, title: '图标', templet: '#icon'},
	                {field: 'key', align: 'left', width: 150, title: '代码'}
	            ]],
	            parseData: function (res) {
	            	for (var i = 0; i < res.data.length; i++) {
	            		for (var j = 0; j < beanIds.length; j++) {
	            			if (beanIds[j] == res.data[i].id) {
	            				console.log(res.data[i]);
	            				res.data[i].LAY_CHECKED = 'true';
	            				break;
	            			}
	            		}
	            	}
	            	
	            	return res;
	            },
	            done: function(res, curr, count) {
	            	//关闭加载
	                layer.closeAll('loading');
	            }
	        });
	    };
	    
	    window.renderTable();
	    
	    window.checkData = function() {
	        var data = table.checkStatus("bean-treetable").data;
	        
	        var ids="";
	        for(var i = 0; i < data.length; i++){
	            if(i > 0) ids += ",";
	            ids += data[i].id;
	        }
	        $("[name='bean.resourceIds']").val(ids);
	    }
	});
	</script>
</body>
</html>